package com.y2w.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.y2w.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122672566";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCocH9FRnTiefaoR2BTV81WXkGzeykHISLtqVEyfsH8w1NXGfazIdbZDOeMFi1tomPBYkONwZn4GCz/VnrSJBB439vUdU5uzLeo4BdIo0WCS3flvVt4NmJLSPs7e2w1QHoiwf1hLsnr3HqRDCu8tr29ya07NsQ33O/1ELe478lT9Cia+y2fdSZrjgPjcCjK3dvr8vLjHofgcVe4ykpbaUAQDTm2FXeSnKHUvW5Rt1Nu3o0aA2A1Toi23pJzzlgjV87ID2B7nEn6KYnvCpdb8QPIWSjNXhQoHPG9y5v8z/N189+0TT9T5jiUR92v3vGI0j3uABRvL0awiT7/4euWm4p9AgMBAAECggEAJk4vI1n6iuahqXNR/KIjGFknJ+ICWpS2lMXEqW8aPplNAqns6J/xdDrzDXCdtCkR9pCaPEqi8UDOOuM320JIXLTMEWz8AUadHdKYePRoglEXWy/M9XGouOZH/Cb9XL7lB7KGJOu3D+R8GzBhYfFNBTQwKR8zUGD2UQ0UOwKNDoJAxX51lO9RvIrRgq3Y+PrA2IMxhR9Esg7lSQgulRdzRewWb46+XC9vLeak0ptTiPQ8bWPTR3ycFDjLAjt/N88JaTmZaZsQ8BWXkuRi0utcO6zE0/ZU3FxI4I9dPAKFIBQ4wQlzZxKHg1vFwvFzthYYmrdcC3+9rH3YE1iP09UfIQKBgQDuA57HSiAiCyAoM7nk9jEmh4jzbOTk3LToUmXcR/EP8LlpgtqdR+8ztqQ/9bG97bhn5Ev9kw5T7eZWkO9QerlB72EQ+tDRT3WNAHL9P6IAL6wBvc9NRojiyEvXi71FX691Jx2cHv8etYbpBi9ronur/6An3LGdyscYMNCyBjnUAwKBgQC1KvSZqtsnoBxEtnTLM9ZzYMWeB96AOfPS41gRyBBMMyA1aSTnBVJntHcf6N4KgbPc5xylomPfTM1f1h4T1OVvKScwQV7fH2RLYumtOQ8htNQrTtnKRS7VKZMLA48oEJw1a3tpwlYEeFMkx4SJNKHlDUsPA4Q8RMmhf36NCRUffwKBgDDgSXC/Rg33nujihmAANmOlHXhveimNMREsKoKru1L8qfaCyoXVycci0GPZ/l/0OxQKi5hM5HpTtiM8rAgoy3ZAw/wKpWr2GDuWRga/VHMZVwcchT9nJqD7ZKekqH9ggNVb7PasXtNdj5C2lnJR8XeGjMrixQ6UH4JWM+T65lElAoGABNfZIiKHKVAGcW/PP39rRDpaDhd3w0XJVuHtAMywYtkbC1J8QhDKWytf4+J9zRoajNVA1sy4jsem1eELfMw8NfSLr6aPurdEHx/Nz2uvE9+ZrG63M8kVr20lGS5HLXMEOgldiz4+vm/ZCSqsSF631T+9ufEGH/ZWzZJy96I+hlECgYEAiDxOGqPvgqVkxiTLp/nvO3uSduT+WT5to3k1VOSqiLuBV+k7Md8z7Ak/eUFonQQ0JKhRqC2sIufLH3Lx0hwxBLDWmLERvJxqiaFKXWcp+Cj4dAZfjoxGGgByuv+R3gT2FPLMs7QsVz2YiHDCZR/FHST4UNDg+rS/8UmTp2wna8g=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkA/xVyu8IQGMUtgerzdWI1Uzw1esNY53kIp5LMZJT9JuGkYhNxLnNvjX3qg+OIk7Z4t1wOEqoABnldlJnwkdYzlbXUDs+v6wrcdFg6mkDq3YQHmoaRc10nPk0wD2oGIM/BpW8jCyk6Hcoi/CUz0m/q3+EZUiZ//r8+IhG3t7Pgtf2Eya78dj5mwJ4kITISCskGuIULq3UEE9XuoxX6k2uNvZn9M7MmGYQJTwwUrCSk8FhTzmuZGdJL7FmISR+IaGM9oGNYGtkBsrvxJMDX82E+OOHd1Je205nQkbodO15XUiJtybGQckGVeXD28QmJeln5BhYisqnb0BOweisDpuOwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="https://5k677q5222.goho.co/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        //System.out.println("支付宝的响应："+result);

        return result;

    }
}
